package com.bodu.system.controller;

import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.request.CodeGenRequest;
import com.bodu.system.entity.vo.CodeGenVo;
import com.bodu.system.service.CodeGenService;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = "代码生成工具")
@RestController
@RequestMapping("/codeGen")
public class CodeGenController extends AbstractCommonController<CodeGenService, CodeGenRequest, CodeGenVo> {

    private final CodeGenService codeGenService;

    public CodeGenController(CodeGenService codeGenService) {
        this.codeGenService = codeGenService;
    }

    @ApiOperation("详情")
    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(codeGenService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("生成代码")
    public Result<Integer> save(@RequestBody CodeGenRequest codeGenRequest) {
        return super.save(codeGenService, codeGenRequest);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新代码")
    public Result<Integer> update(@RequestBody CodeGenRequest codeGenRequest) {
        return super.update(codeGenService, codeGenRequest);
    }

    @TraceLog()
    @PostMapping("createCodeGen")
    @ApiOperation("生成代码")
    public Result<Integer> createCodeGen(@RequestBody CodeGenRequest codeGenRequest) throws TemplateException, IOException {
        codeGenRequest.setCreateUser(LoginUserUtil.getUserId());
        return codeGenService.createCodeGen(codeGenRequest);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("数据表")
    public Result<List<CodeGenVo>> listPage(@RequestBody ParamReq<CodeGenRequest> req)  {
        return super.listPage(codeGenService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("数据表-统计")
    public Result<Integer> countPage(@RequestBody ParamReq<CodeGenRequest> req)  {
        return super.countPage(codeGenService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("数据表-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<CodeGenRequest> req)  {
        return super.page(codeGenService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除生成代码")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(codeGenService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除生成代码")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(@PathVariable("ids") List<Long> ids) {
        return super.batchDelete(codeGenService, ids);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}
